package cn.thoughtworks.school.repositories;

import cn.thoughtworks.school.entities.OnlineLanguageQuiz;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Optional;

public interface OnlineLanguageQuizRepository extends JpaRepository<OnlineLanguageQuiz, Long>,JpaSpecificationExecutor<OnlineLanguageQuiz> {
    List<OnlineLanguageQuiz> findByIdInAndIsAvailableIsTrue(List<Long> ids);

    Page<OnlineLanguageQuiz> findAllByIsAvailableIsTrueAndQuizGroupIdIn(List quizGroupIds, Pageable pageable);

    Page<OnlineLanguageQuiz> findAllByIsAvailableIsTrueAndStatusAndQuizGroupIdIn(int status, List quizGroupIds, Pageable pageable);

    Page<OnlineLanguageQuiz> findAllByIsAvailableIsTrueAndMakerIdAndStatusAndQuizGroupIdIn(Long makerId, int status, List quizGroupIds, Pageable pageable);

    Page<OnlineLanguageQuiz> findAllByIsAvailableIsTrueAndMakerIdAndQuizGroupIdIn(Long makerId, List quizGroupIds, Pageable pageable);

    @Query(value = "SELECT distinct b FROM OnlineLanguageQuiz b JOIN b.tags t WHERE b.isAvailable=true AND b.quizGroupId in:quizGroupIds  AND t.name in :names")
    Page<OnlineLanguageQuiz> findAllByIsAvailableAndTagAndQuizGroupIdIn(@Param("names") String[] names, @Param("quizGroupIds") List quizGroupIds, Pageable pageable);

    @Query(value = "SELECT distinct b FROM OnlineLanguageQuiz b JOIN b.tags t WHERE (b.isAvailable=true AND b.quizGroupId in:quizGroupIds AND t.name in :names) AND b.status=:status")
    Page<OnlineLanguageQuiz> findAllByIsAvailableAndTagAndStatusAndQuizGroupIdIn(@Param("names") String[] names, @Param("status") int status, @Param("quizGroupIds") List quizGroupIds, Pageable pageable);

    Page<OnlineLanguageQuiz> findAllByIdAndStatusAndQuizGroupIdIn(Long id, int status, List quizGroupIds, Pageable pageable);

    Page<OnlineLanguageQuiz> findByRemarkLikeAndIsAvailable(String remark,Boolean available,Pageable pageable);

    Page<OnlineLanguageQuiz> findByDescriptionLikeAndIsAvailable(String description,Boolean available,Pageable pageable);

    Page<OnlineLanguageQuiz> findAllByIdAndQuizGroupIdIn(Long id, List quizGroupIds, Pageable pageable);

    Page<OnlineLanguageQuiz> findByRemarkLikeAndIsAvailableAndQuizGroupIdIn(String s, boolean b, List quizGroupIds, Pageable pageable);

    Page<OnlineLanguageQuiz> findByDescriptionLikeAndIsAvailableAndQuizGroupIdIn(String s, boolean b, List quizGroupIds, Pageable pageable);

    Page<OnlineLanguageQuiz> findByRemarkLikeAndIsAvailableAndStatusAndQuizGroupIdIn(String s, boolean b, int status, List quizGroupIds, Pageable pageable);

    Page<OnlineLanguageQuiz> findByDescriptionLikeAndIsAvailableAndStatusAndQuizGroupIdIn(String s, boolean b, int status, List quizGroupIds, Pageable pageable);

    Page<OnlineLanguageQuiz> findByOnlineLanguageNameLikeAndIsAvailableAndQuizGroupIdIn(String s, boolean b, List quizGroupIds, Pageable pageable);

    Page<OnlineLanguageQuiz> findByOnlineLanguageNameLikeAndIsAvailableAndStatusAndQuizGroupIdIn(String s, boolean b, int status, List quizGroupIds, Pageable pageable);

    Optional<OnlineLanguageQuiz> findByIdAndIsAvailableIsTrue(Long quizId);

    @Query(value = "SELECT  b FROM OnlineLanguageQuiz b JOIN b.tags t WHERE  b.isAvailable=true AND t.name in :names")
    List<OnlineLanguageQuiz> findQuizzesByTags(@Param("names") List<String> names);

    List<OnlineLanguageQuiz> findByIdIn(List<Long> ids);
}
